package service.impl;

import core.Page;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import domain.AdminUser;
import service.AdminUserService;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 于金鹏
 * @Date 2024/12/20 9:36
 * @Package StudentCheckSystem
 */
public class AdminUserServiceImpl implements AdminUserService {
    private UserDao userDao = new UserDaoImpl();

    public AdminUserServiceImpl() {
    }

    public AdminUserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public boolean login(String username, String password) throws SQLException {
        AdminUser adminUser = userDao.getUserByUsername(username);
        if (adminUser != null && adminUser.getPassword().equals(password)) {
            return true;
        }
        return false;
    }

    @Override
    public void register(AdminUser adminUser) throws SQLException {
        userDao.addUser(adminUser);
    }

    @Override
    public int addUser(AdminUser adminUser) throws SQLException {
        return this.userDao.addUser(adminUser);
    }

    @Override
    public int deleteUser(int id) throws SQLException {
        return this.userDao.deleteUserByUserId(id);
    }

    @Override
    public int updateUser(AdminUser adminUser) throws SQLException {
        return this.userDao.updateUserByUserId(adminUser);
    }

    @Override
    public AdminUser getUserById(int id) throws SQLException {
        return this.userDao.getUserByUserId(id);
    }

    @Override
    public Page<AdminUser> getUserPage(int pageNum, int pageSize, AdminUser adminUser) throws SQLException {
        int total = this.userDao.count(adminUser);
        List<AdminUser> records = this.userDao.UserPage(adminUser, pageNum, pageSize);
        return new Page<>(total, records);
    }
}
